package org.jeecg.modules.zixishi.job;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.meituan.sdk.MeituanClient;
import com.meituan.sdk.auth.MeituanTokenData;
import com.meituan.sdk.auth.MeituanTokenResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.voucher.entity.Voucher;
import org.jeecg.modules.voucher.service.IVoucherService;
import org.jeecg.modules.zixishi.entity.ThirdToken;
import org.jeecg.modules.zixishi.service.IThirdTokenService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
public class MeituanTokenJob implements Job {

	@SneakyThrows
    @Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		ISysUserService sysUserService = SpringContextUtils.getApplicationContext().getBean(ISysUserService.class);
		IThirdTokenService thirdTokenService = SpringContextUtils.getApplicationContext().getBean(IThirdTokenService.class);
		ISysDictService sysDictService = SpringContextUtils.getApplicationContext().getBean(ISysDictService.class);
		RedisUtil redisUtil = SpringContextUtils.getApplicationContext().getBean(RedisUtil.class);
		MeituanClient meituanClient = SpringContextUtils.getApplicationContext().getBean(MeituanClient.class);

		List<DictModel> configs = sysDictService.getDictItems("voucher_config");

		ThirdToken thirdToken = thirdTokenService.getOne(new LambdaQueryWrapper<ThirdToken>().eq(ThirdToken::getThirdType,"meituan"));
		MeituanTokenResponse response = meituanClient.refreshToken(58,thirdToken.getRefreshToken());
		if(response.isSuccess()){
			MeituanTokenData data = response.getData();
			thirdToken.setAccessToken(data.getAccessToken());
			thirdToken.setRefreshToken(data.getRefreshToken());
			thirdTokenService.updateById(thirdToken);
			redisUtil.set("meituantoken",thirdToken.getAccessToken());
		}
	}
}